package com.example.androidlesson.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.androidlesson.interfaces.ICustomerNotifyDataChangedEvent;
import com.example.androidlesson.interfaces.IItemDataBind;

import java.util.ArrayList;

/**
 * ListView   GridView  数据绑定对象
 * **/
public class NewCustomerItemAdapter extends BaseAdapter {

    private final ArrayList<Object> arr=new ArrayList<Object>();

    private final ArrayList<ViewBinderPosition>  viewbind= new ArrayList<>();

    private IItemDataBind mBind=null;
    private Context mcon=null;

    private ICustomerNotifyDataChangedEvent _notifyDataChanged=null;


    public NewCustomerItemAdapter(Context con){
        mcon=con;
    }


    /**
     * 设置数据变化通知事件
     * @param notifyDataChanged
     */
    public void setNotifyDataChangedEvent(ICustomerNotifyDataChangedEvent notifyDataChanged){
        _notifyDataChanged=notifyDataChanged;
    }

    /**
     * 添加数据对象
     * @param value
     * @return
     */
    public boolean Add(Object value){
        boolean R= arr.add(value);
        this.notifyDataSetChanged();
        return R;
    }

    /**
     * 插入数据对象
     * @param index
     * @param value
     */
    public void InsertAt(int index,Object value){
        arr.add(index, value);
        this.notifyDataSetChanged();
    }
    /**
     * 移除指定索引处的对象
     * @param index   指定索引处
     * @return  返回被移除的对象
     */
    public Object Remove(int index){
        Object R=arr.remove(index);
        this.notifyDataSetChanged();
        return R;
    }

    /**
     * 移除指定对象
     * @param object
     * @return
     */
    public boolean Remove(Object object){
        boolean R=arr.remove(object);
        this.notifyDataSetChanged();
        return R;
    }

    /**
     * 设置绑定接口  用于绑定数据到界面
     * @param IBinder
     */
    public void SetBinder(IItemDataBind IBinder){
        this.mBind=IBinder;
    }

    /**
     * 清空集合里面所有的数据
     */
    public void Clear(){
        arr.clear();
        this.notifyDataSetChanged();
    }

    /**
     * 获取集合数据的大小
     */
    public int getCount() {
        // TODO Auto-generated method stub
        return arr.size();
    }

    /**
     * 获取指定位置的对象数据
     */
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arr.get(position);
    }

    /**
     *
     * 该函数可以不用返回0
     */
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    /**
     * 获取对应的位置关系
     * @param v
     * @return
     */
    private ViewBinderPosition getEntityByView(View v){
        if (viewbind.size()>0){
            for (int i=0;i<viewbind.size();i++){
                ViewBinderPosition curr=viewbind.get(i);
                if (curr.get_view()==v){
                    return curr;
                }
            }
        }
        return null;
    }

    /**
     * 获取指定view的绑定位置
     * @param v
     * @return
     */
    public int getViewBindPosition(View v){
        ViewBinderPosition curr= getEntityByView(v);
        if (curr!=null){
            return curr.getPosition();
        }
        return -1;
    }

    /**
     * 通过View子id来查找指定View的绑定对象
     * @param subViewId
     * @param subView
     * @return
     */
    public Object findSubViewBandObjectBySubViewId(int subViewId,View subView){
        if ((viewbind.size()>0) && (subView!=null)){
            for (int i=0;i<viewbind.size();i++){
                ViewBinderPosition curr=viewbind.get(i);
                if (curr.get_view().findViewById(subViewId)==subView){
                    if (curr.getPosition()>-1 && curr.getPosition()<this.getCount()){
                        return this.getItem(curr.getPosition());
                    }
                }
            }
        }
        return null;
    }

    public void  removeAtNoRefesh(int index) {
        arr.remove(index);
    }

    public void addAtNoRefresh(int index,Object value) {
        arr.add(index, value);
    }

    /**
     * 查找当前指定选项的根视图
     * @param subViewId
     * @param subView
     * @return
     */
    public View getItemRootView(int subViewId,View subView){
        if ((viewbind.size()>0) && (subView!=null)){
            for (int i=0;i<viewbind.size();i++){
                ViewBinderPosition curr=viewbind.get(i);
                if (curr.get_view().findViewById(subViewId)==subView){
                    return curr.get_view();
                }
            }
        }
        return null;
    }

    public View getItemView(int index){
        if (viewbind.size()>0){
            for (int i=0;i<viewbind.size();i++){
                ViewBinderPosition curr=viewbind.get(i);
                if (index==i){
                    return curr.get_view();
                }
            }
        }
        return null;
    }

    /**
     * 获取指定view的绑定 对象
     * @param v
     * @return
     */
    public Object getViewBindObject(View v){
        int pos=getViewBindPosition(v);
        if (pos>-1 && pos<this.getCount()){
            return this.getItem(pos);
        }
        return null;
    }


    @Override
    public void notifyDataSetChanged() {
        // TODO Auto-generated method stub
        if (_notifyDataChanged!=null){
            _notifyDataChanged.BeforeDataChanged();
        }
        super.notifyDataSetChanged();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View V=null;
        if (convertView!=null){
            V=convertView;
        }else{
            if (this.mBind!=null){
                V=this.mBind.GetItemView(arr.get(position),mcon);
                viewbind.add(new ViewBinderPosition(position, V));
            }
        }

        if (this.mBind!=null){
            this.mBind.Binder(arr.get(position),position, V, null);
            ViewBinderPosition curr=getEntityByView(V);
            if (curr!=null){
                curr.setPosition(position);
            }
        }

        return V;
    }


    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View V=null;
        if (convertView!=null){
            V=convertView;
        }else{
            if (this.mBind!=null){
                V=this.mBind.GetItemDropdownItemView(arr.get(position),mcon);
                viewbind.add(new ViewBinderPosition(position, V));
            }
        }

        if (this.mBind!=null){
            this.mBind.Binder(arr.get(position),position, null, V);
            ViewBinderPosition curr=getEntityByView(V);
            if (curr!=null){
                curr.setPosition(position);
            }
        }

        //this.getvie

        return V;
    }

    /**
     * 是否为空集合
     */
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return arr.isEmpty();
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        if (this.mBind!=null){
            return	this.mBind.getItemViewType(arr.get(position));
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        //Log.e("dd", msg)
        if (this.mBind!=null){
            int c= this.mBind.getViewTypeCount();
            if (c<1){
                return 1;
            }else{
                return c;
            }
        }
        return super.getViewTypeCount();
    }
}


/**
 * View绑定的位置关系
 * @author 刘
 *
 */
class ViewBinderPosition{

    private int position=-1;
    private View _view=null;
    public ViewBinderPosition(int position, View _view) {
        super();
        this.position = position;
        this._view = _view;
    }
    public int getPosition() {
        return position;
    }
    public void setPosition(int position) {
        this.position = position;
    }
    public View get_view() {
        return _view;
    }
    public void set_view(View _view) {
        this._view = _view;
    }

}